<?php

class model_firadio_php_sys_procedure extends db {

    public function __construct() {
        $db = db::getInstance();
        $this->firphp_procedure = $db->table("firphp_procedure");
        $db = db::getInstance();
        //$this->firphp_procgroup = $db->table("firphp_procgroup");
    }

    public function getProcTextByItemId($menuitemid, $viewnameid) {
        //根据数据库的记录，生成程序代码的JSON数组
        $rows = $this->firphp_procedure->field("id,parentid,typename,proctext,level,title")->where("menuitemid='{0}' AND enable=1 AND del_time='0' AND viewnameid='{1}'", array($menuitemid, $viewnameid))->orderby("level,orderid")->select();
        $proc = $procs = array();
        foreach ($rows as $row) {
            $proc[$row['id']] = array();
            $proc[$row['id']]['id'] = $row['id'];
            $proc[$row['id']]['typename'] = $row['typename'];
            $proc[$row['id']]['note'] = $row['title'];
            $proc[$row['id']]['proctext'] = json_decode($row['proctext'], true);
            if ($row['parentid'] == 0 && $row['level'] == 0) {
                $procs[] = &$proc[$row['id']]; //采用地址引用方式传递给$menus
            } else {
                $proc[$row['parentid']]['sub'][] = &$proc[$row['id']];
            }
        }
        //print_rr($procs);
        $return = json_encode($procs);
        return($return);
    }

    /*
      public function getProcGroupList() {
      $rows = $this->firphp_procgroup->field("id,title,name,class")->where("enable='1'", array())->select();
      return($rows);
      }

      public function getProcGroupId($name) {
      $rows = $this->firphp_procgroup->field("id")->where("enable='1' AND name='{0}'", array($name))->getvalue();
      $rows = intval($rows);
      return($rows);
      }
     */

    public function setOrderId($menuitemid, $parentid) {
        //这里的WHERE语句不能加入 AND del_time='0'  因为要保留对删除的排序
        $where = "menuitemid='{0}' AND parentid='{1}'";
        $wherep = array($menuitemid, intval($parentid));
        /*
          $procgroupid = $this->getProcGroupId($parentid);
          if ($procgroupid > 0) {
          $where.=" AND `groupid`='{2}'";
          $wherep[] = $procgroupid;
          } else {
          $wherep[] = 0;
          }
         */
        $nowid = 0;
        $rows = $this->firphp_procedure->field("id,previd,nextid")->where($where, $wherep)->select();
        $procids = array();
        //先把【过程项】都放到$procids数组，并找出第一个$nowid
        foreach ($rows as $row) {
            $procids[$row['id']] = $row;
            if ($nowid == 0 && $row['previd'] == 0) {
                $nowid = $row['id'];
            }
        }
        $orderid = 0;
        //只要$nowid不为0就继续循环，直到nextid值返回0为止
        while ($nowid != 0) {
            //echo "[\$nowid=$nowid]";
            $orderid++;
            $where1 = $where . " AND id='{2}'";
            $wherep1 = $wherep;
            $wherep1[] = $nowid;
            $this->firphp_procedure->where($where1, $wherep1)->update(array('orderid' => $orderid));
            $nowid = $procids[$nowid]['nextid'];
        }
    }

    public function getProcListByItemId($menuitemid, $parentid) {
        //获取程序过程代码列表时先做排序
        $this->setOrderId($menuitemid, $parentid);
        //提供菜单项ID号，上级ID号，非删除的作为查询条件
        $where = "menuitemid='{0}' AND parentid='{1}' AND del_time='0'";
        $wherep = array($menuitemid, intval($parentid));
        /*
          $procgroupid = $this->getProcGroupId($parentid);
          if ($procgroupid > 0) {
          $where.=" AND `groupid`='{2}'";
          $wherep[] = $procgroupid;
          }
         */
        //获取列表
        $rows = $this->firphp_procedure->field("id,title,parentid,typename,proctext,enable")->where($where, $wherep)->orderby("orderid")->select();
        return($rows);
    }

    public function getProcById($id) {
        $rows = $this->firphp_procedure->field("id,title,parentid,typename,proctext,enable")->where("id='{0}' AND del_time='0'", array($id))->getone();
        return($rows);
    }

    public function setProc($id, $post) {
        $row = array();
        $row['enable'] = intval($post['enable']);
        $row['title'] = trim($post['title']);
        $row['typename'] = trim($post['typename']);
        $row['proctext'] = $post['proctext'];
        $return = $this->firphp_procedure->where("id='{0}' AND del_time='0'", array($id))->update($row);
        return($return);
    }

    public function addProc($post) {
        $row = array();
        $row['menuitemid'] = intval($post['menuitemid']);
        $row['parentid'] = intval($post['parentid']);
        $row['viewnameid'] = intval($post['viewnameid']);
        /*
          $row['groupid'] = 0;
          if ($row['parentid'] == 0) {
          $row['groupid'] = $this->getProcGroupId($post['parentid']); //获取上级的groupid名字
          } else {
          $temprow = $this->firphp_procedure->field("groupid,viewnameid")->where("id='{0}' AND del_time='0'", array($row['parentid']))->getone();
          $row['groupid'] = $temprow['groupid'];
          $row['viewnameid'] = $temprow['viewnameid'];
          }
          if ($row['groupid'] == 0) {
          //记录错误日志：TYPE值无法获取
          return(-1);
          }
         */
        $row['enable'] = intval($post['enable']);
        $row['title'] = trim($post['title']);
        $row['typename'] = trim($post['typename']);
        $row['proctext'] = $post['proctext'];
        $row['level'] = 0;
        if ($row['parentid'] > 0) {
            $ret = $this->firphp_procedure->field("level")->where("id='{0}' AND del_time='0'", array($row['parentid']))->getone();
            if ($ret) {
                $row['level'] = intval($ret['level']) + 1;
            }
        }
        $selid = $row['nextid'] = intval($post['nextid']);
        $row['previd'] = 0;
        if ($row['nextid'] == 0) {
            //表示要在语句块的最末尾去添加
            //首先找出最末尾的ID号,并赋值给previd
            $row['previd'] = intval($this->firphp_procedure->field("id")->where("menuitemid='{0}' AND parentid='{1}' AND viewnameid='{2}' AND nextid='0' AND del_time='0'", array($row['menuitemid'], $row['parentid'], $row['viewnameid']))->getvalue());
        } else {
            //如果要在中间加，或者最顶上加的话
            //1：首先找出选中ID($selid)的这条记录的previd号。命名为oldprevid,也是我们要的$row['previd']
            $row['previd'] = intval($this->firphp_procedure->field("previd")->where("id='{0}' AND del_time='0'", array($selid))->getvalue());
        }
        //至少要有一个有值才行
        $insertid = $this->firphp_procedure->insert($row);
        //添加后，将修改选中ID的这条记录里面的previd号，改成$insertid
        if ($row['nextid'] > 0) {
            if (!$this->firphp_procedure->where("id='{0}' AND del_time='0'", array($selid))->update(array('previd' => $insertid))) {
                return(-2);
            }
        }
        if ($row['previd'] > 0) {
            if (!$this->firphp_procedure->where("id='{0}' AND del_time='0'", array($row['previd']))->update(array('nextid' => $insertid))) {
                return(-3);
            }
        }
        return($insertid);
    }

    public function delProc($id) {
        return $this->firphp_procedure->where("id='{0}' AND del_time='0'", array($id))->update(array('del_time' => microtime(true)));
    }

}

?>
